package com.bwhx.eruler.controller;

import java.io.FileNotFoundException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bwhx.eruler.common.springdao.SqlDao;
import com.bwhx.eruler.util.file.FileUtil;


/**
 * @Description:
 * @Author:	nutony
 * @CreateDate:	2014-4-6
 */
@Controller
public class CountController {
	@Autowired
	SqlDao sqlDao;
	
	//生产厂家销售情况统计饼图
	@RequestMapping("/page.sellermonth.ashx")
	public String factorySale(HttpServletRequest request) throws FileNotFoundException{
		/*
		 * 操作步骤：
		 * 1、获取数据
		 * 2、生成xml文件，覆盖掉指定目录下的data.xml
		 * 3、转向对应的页面index.html
		 */
	/*	String sql = "SELECT f.factory_name,cp.countnum FROM (SELECT factory_id,factory_name FROM factory_c) f RIGHT JOIN ( SELECT factory_id,COUNT(*) AS countnum FROM contract_product_c GROUP BY factory_id ) cp ON f.factory_id=cp.factory_id";
		sql = "SELECT product_no,COUNT(*) AS countnum FROM contract_product_c GROUP BY product_no ORDER BY countnum DESC LIMIT 10";
		String path = request.getSession().getServletContext().getRealPath("/");
		this.writeXml(path, "factorysale", this.getPieXml(this.getData(sql)));
		*/
		return "/count/jStat.jsp?forward=run/count/sellermonth";
	}
	
	//年度排行
	@RequestMapping("/page.ruleryearcount.ashx")
	public String productSale(HttpServletRequest request) throws FileNotFoundException{
		
		/*String sql = "SELECT product_no,COUNT(*) AS countnum FROM contract_product_c GROUP BY product_no ORDER BY countnum DESC LIMIT 10";
		String path = request.getSession().getServletContext().getRealPath("/");
		this.writeXml(path, "productsale", this.getColumneLineXml(this.getData(sql)));*/
		
		return "/count/jStat.jsp?forward=run/count/yearcount";
	}
	
	//销售员排行
	@RequestMapping("/page.selleryearcount.ashx")
	public String salerYear(HttpServletRequest request) throws FileNotFoundException{
		
		/*String sql = "SELECT product_no,COUNT(*) AS countnum FROM contract_product_c GROUP BY product_no ORDER BY countnum DESC LIMIT 10";
		String path = request.getSession().getServletContext().getRealPath("/");
		this.writeXml(path, "productsale", this.getColumneLineXml(this.getData(sql)));*/
		
		return "/count/jStat.jsp?forward=run/count/selleryear";
	}
	
	//获得数据源
	private List<String> getData(String sql){
		return sqlDao.executeSQL(sql);
	}
	
	//拼接饼形图xml文件
	private String getPieXml(List<String> dataList){
		StringBuffer sBuf = new StringBuffer();
		sBuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sBuf.append("<pie>");
		for(int i=0;i<dataList.size();){
			sBuf.append("<slice title=\"").append(dataList.get(i++)).append("\">").append(dataList.get(i++)).append("</slice>");
		}
		sBuf.append("</pie>");
		return sBuf.toString();
	}
	
	//拼接柱状图xml文件
	private String getColumneLineXml(List<String> dataList){
		int xid = 0;
		StringBuffer sBuf = new StringBuffer();
		sBuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		sBuf.append("<chart>");
		sBuf.append("	<series>");
		
		
		for(int i=0;i<dataList.size();){
			sBuf.append("		<value xid=\"").append(xid++).append("\">").append(dataList.get(i++)).append("</value>");
			i++;
		}
		sBuf.append("	</series>");
		sBuf.append("	<graphs>");
		sBuf.append("		<graph gid=\"30\" color=\"#FFCC00\" gradient_fill_colors=\"#111111, #1A897C\">");
		
		xid = 0;			//初始化
		for(int i=0;i<dataList.size();){
			i++;
			sBuf.append("			<value xid=\"").append(xid++).append("\">").append(dataList.get(i++)).append("</value>");
		}
		sBuf.append("	</graph>");
		sBuf.append("	</graphs>");
		sBuf.append("</chart>");
		
		return sBuf.toString();
	}
	
	//写xml文件
	private void writeXml(String path, String dir, String content) throws FileNotFoundException{
		FileUtil fu = new FileUtil();
		fu.newTxt(path+"/run/stat/"+dir+"/", "data.xml", content, "utf-8");
	}
	
	//系统访问压力图
	@RequestMapping(value = "/page.rulermonthcount.ashx")
	public String onlineLine(HttpServletRequest request) throws FileNotFoundException{
	/*	String sql = "SELECT t.a1,login_log_p.countnum FROM (SELECT A1 FROM online_t) t LEFT JOIN ( SELECT SUBSTRING(login_time,12,2) AS A1,COUNT(*) AS countnum FROM login_log_p  GROUP BY A1 ) login_log_p ON t.a1=login_log_p.a1";
		String path = request.getSession().getServletContext().getRealPath("/");
		this.writeXml(path, "onlineinfo", this.getColumneLineXml(this.getData(sql)));*/
		
		return "/count/jStat.jsp?forward=run/count/monthcount";
	}
	
}
